<template>
  <router-view v-slot="{ Component }" class="view">
    <transition :name="transitionName" mode="out-in">
      <keep-alive :include="['Home', 'Detail']">
        <component :is="Component" />
      </keep-alive>
    </transition>
  </router-view>
</template>

<script>
import { defineComponent, ref } from "vue";
import { useRouter } from "vue-router";

export default defineComponent({
  setup() {
    const transitionName = ref("slide-left");
    const router = useRouter();
    router.beforeEach((to, from) => {
      if (to.meta.index > from.meta.index) {
        //设置动画名称
        transitionName.value = "slide-left";
      } else {
        transitionName.value = "slide-right";
      }
    });
    return { transitionName };
  },
});
</script>

<style lang="scss">
.view {
  width: 100%;
  position: absolute;
}
.slide-right-enter-active,
.slide-right-leave-active,
.slide-left-enter-active,
.slide-left-leave-active {
  will-change: transform;
  transition: all 300ms;
  position: absolute;
}
.slide-right-enter {
  opacity: 0;
  transform: translate3d(-100%, 0, 0);
}
.slide-right-leave-active {
  opacity: 0;
  transform: translate3d(100%, 0, 0);
}
.slide-left-enter {
  opacity: 0;
  transform: translate3d(100%, 0, 0);
}
.slide-left-leave-active {
  opacity: 0;
  transform: translate3d(-100%, 0, 0);
}
</style>